<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/header.jsp" %>
<%@ include file="/common/datatables.jsp" %>

<script type="text/javascript">	
	var chooseUserListTable;
	var hideCol = [2,3,4];
	$(function(){
		
 		Orginput.init(
 			$("#choosedOrg"),{
					defaultOrgId:'${_belongArea}',
					defaultOrgName:'<hw:orgName orgId="${_belongArea}" />',
					checkType:"org",
					afterSelect:function(obj){
						getDeptList($("#choosedOrg").val());
					}
			}
		);
			 
		if('${_belongArea}' != ''){
 			$("#choosedOrg").setDisable(true);
 		} 
 	 	if('${_belongDept}' != ''){
 			$("#choosedDeptId").empty();
 		}else if('${_belongArea}' != ''){
 			getDeptList('${_belongArea}');
 		}
 	 	if('#request.chooseRoleList' != ''){
 	 		$("#rolesTable input").attr("disabled", true);
 		} 
 		
		
		});
		
			 
	
		chooseUserListTable = $('#chooseUserListTab').DataTable({
	    	"bProcessing": true,
			"bServerSide": true,
			"bDestroy":true,
			"bSort": false,
			"bFilter": false,
			"bAutoWidth": false,
			"sPaginationType": "full_numbers",
			"bLengthChange":false,
			"fnServerParams": function ( aoData ) {
				var newParam = changeParam();
				aoData.push( { "name": "userId", "value": newParam.divUserId==''?"${userOrgDeptRole.userId}":newParam.divUserId } );
				aoData.push( { "name": "userName", "value": newParam.divUserName==''?"${userOrgDeptRole.userName}":newParam.divUserName } );
				aoData.push( { "name": "cardNo", "value": "${userOrgDeptRole.cardNo}"});
				aoData.push( { "name": "queryRole", "value": newParam.userRoles==''?"${userOrgDeptRole.queryRole}":newParam.userRoles} );
				aoData.push( { "name": "orgId", "value": newParam.choosedOrg==''?"${userOrgDeptRole.orgId}":newParam.choosedOrg } );
				aoData.push( { "name": "deptId", "value": newParam.choosedDeptId==''?"${userOrgDeptRole.deptId}":newParam.choosedDeptId } );
				aoData.push( { "name": "status", "value": "${userOrgDeptRole.status}" } );
			},
			 "columnDefs": [//定义列的显示隐藏
	            {
                "targets": [ 1 ],
                "visible": false,
	            },
	        ],
			"sAjaxSource": "${ctx}/user/selectUserList?isSelect=1",
			"fnServerData": fnDataTablesPipeline,
			"sServerMethod": "POST",
			"oLanguage":oLanguage
		});
	
	
	  //点击查询按钮时
	  $("#btnQuery").click(function(){
	  	 oCache.iCacheLower = -1;//消除缓存，不设置这个，导致下面的重新加载不进入后台程序
		 chooseUserListTable.ajax.reload();//重新加载datatable
	  });
	
	//选中一笔记录时
	function setChooseUserRetValue(userId,userName,belongArea,belongDept,userRole){		
		var retValue="{userId:'"+userId+"',userName:'"+userName+"',userRole:'"+userRole+"',belongArea:'"+belongArea+"',belongDept:'"+belongDept+"'}";	
		$("#retValue").val(retValue);		
	}
	//获得指定机构下的部门
	function getDeptList(orgId){
			var params = {"orgId":orgId};
			$.ajax({
				url : "${ctx}/component/queryDeptList.do",
				async : false,
				data : $.param(params),
				type : "POST",
				dataType : "json",
				success : function(data, textStatus) {
					//debugger;
					$("#choosedDeptId").empty();
					$("#choosedDeptId").append("<option value=''></option>"); 
					if(data != undefined){
						for(var i=0;i<data.length;i++){
							var dept = data[i];
							$("#choosedDeptId").append("<option value='"+dept.deptId+"'>"+dept.deptName+"</option>"); 
						}
					}
				}		
			});	

	}
	
	function selectUser(userId,userName,userRole,orgId,deptId){
		var retValue="{userId:'"+userId+"',userName:'"+userName+"',userRole:'"+userRole+"',belongArea:'"+orgId+"',belongDept:'"+deptId+"'}";	
		$("#retValue").val(retValue);
	}
	
	
	
	function changeParam(){
		 var userRoles = $("input[name='userRoles']:checked").map(function() {
            return $(this).val();
        }).get().join(","); 
		 /**
		var userRoles = $(".roleCheckStatus input[type='checkbox']:checked").map(function() {
            return $(this).val();
        }).get().join(",");*/
		var jsonObj = formStrToJson($("#userForm").serialize());
		jsonObj.userRoles = userRoles;
		return jsonObj;
	}
	
</script>

<div class="row" id="selectUserDiv">
	<div class="col-xs-12">	
		<div class="widget-box widget-color-blue2">			
			<div class="widget-header">
				<h5 class="widget-title">用户查询</h5>
				<!-- #section:custom/widget-box.toolbar -->
				<div class="widget-toolbar">
					<a href="#" data-action="fullscreen" class="orange2"><i class="ace-icon fa fa-expand"></i></a>
					<a href="#" data-action="collapse"><i class="ace-icon fa fa-chevron-up"></i></a>
					<a href="#" data-action="close"><i class="ace-icon fa fa-times"></i></a>
				</div>
				<!-- /section:custom/widget-box.toolbar -->
			</div>
			<div class="widget-body">
				<div class="widget-main no-padding">				
					<form class="form-horizontal" id="userForm" name="userForm">
						<fieldset>
						    <div class="row">
						    	<div class="form-group col-xs-6">
									<label class="col-sm-3 control-label no-padding-left" for="orgId">用户编号</label>
									<div class="col-sm-9">
										<div class="clearfix">
											 <input type="text" class="col-xs-10 col-sm-9"  name="divUserId" id="divUserId"/>
										</div>
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-3 control-label no-padding-left" for="orgId">用户名称</label>
									<div class="col-sm-9">
										<div class="clearfix">
											 <input type="text" class="col-xs-10 col-sm-9"  name="divUserName" id="divUserName"/>
										</div>
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-3 control-label no-padding-left" for="orgId">所在机构</label>
									<div class="col-sm-9">
										<div class="clearfix">
											<input type="text" class="col-xs-10 col-sm-9"  name="choosedOrg" id="choosedOrg"/>
										</div>
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-3 control-label no-padding-left" for="orgId">所在部门</label>
									<div class="col-sm-9">
										<div class="clearfix">
											<select class="chosen-select form-control col-xs-10"name="choosedDeptId" id="choosedDeptId">
					  						</select>
										</div>
									</div>
								</div>
							</div>
							<div class="row">
						       <div class="form-group col-xs-12">
									<div class="col-sm-12">					     
										     <div class="checkbox">
										     <c:forEach items="${roleList}" var="role" varStatus="i">	
									     		<label>
													<%-- <input type="checkbox" class="ace roleCheckStatus" name="roles[${i.index }].roleId" value="${role.roleId}" />
													 --%>
													 <input type="checkbox" class="ace " name="userRoles" value="${role.roleId}" />
													<span class="lbl">${role.roleName}</span>
												</label>
										     </c:forEach>
											 </div>
									</div>
						         </div>
							</div>
						</fieldset>
						<div class="form-actions center">
							<button class="btn btn-sm btn-info" type="button" id="btnQuery">
								<i class="icon-on-right ace-icon fa fa-search bigger-110"></i>
								查询
							</button>
							&nbsp; &nbsp; &nbsp;
							<button class="btn btn-sm" type="reset">
								<i class="ace-icon fa fa-refresh bigger-110"></i>
								重置
							</button>
						</div>		
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="row">
	<input type="hidden" id="retValue" value="">
	<div>
		<table id="chooseUserListTab" class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						 <th nowrap>选择</th>
						 <th nowrap>编号</th>
			             <th nowrap>用户编号</th>
			             <th nowrap>用户名称</th>
			             <th nowrap>所属机构</th>
			             <th nowrap>所属部门</th>
			             <th nowrap>用户角色</th>			
		            </tr>
				</thead>
				<tbody>
					<tr><td colspan="8" align="center">数据加载中...</td></tr>
				</tbody>
		</table>
	</div>
</div>
